using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using webapp4U.BOL;

namespace webapp4U.UI.Controls
{
    public partial class Controls_MenuLeft : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Load_Menu();
        }

        private void Load_Menu()
        {
            if (!IsPostBack)
            {
                MenuCategory.Items.Clear();
                webapp4U.BOL.Category c = webapp4U.BOL.Category.GetCategoryByID(Convert.ToInt32(eCategory.DanhMucSanPham));
                MenuItem newMenu = new MenuItem();
                newMenu.Text = (Globals.LangVietNam()) ? c.CateName : c.CateName_en;
                newMenu.Value = c.CateID.ToString();
                newMenu.NavigateUrl = "~/Products/Product.aspx?CateID="+ c.CateID.ToString();
                MenuCategory.Items.Add(newMenu);
                populateCategoryNode(newMenu);
            }
        }

        private void populateCategoryNode(MenuItem n)
        {
            List<webapp4U.BOL.Category> ChildMenu = webapp4U.BOL.Category.GetCategoryByParentCateID(Convert.ToInt32(n.Value));
            if (ChildMenu.Count == 0) // base case: leaf category 
            {
                return;
            }
            else
            {
                foreach (webapp4U.BOL.Category c in ChildMenu)
                {
                    MenuItem newMenu = new MenuItem();
                    newMenu.Text = (Globals.LangVietNam())? c.CateName : c.CateName_en;
                    newMenu.Value = c.CateID.ToString();
                    newMenu.NavigateUrl = "~/Products/Product.aspx?CateID=" + c.CateID.ToString();
                    n.ChildItems.Add(newMenu);
                    populateCategoryNode(newMenu);
                }
            }
        }
    }
}